package com.springbootnetty;


import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.springbootnetty.netty_server.MyNettyServer;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.PooledByteBufAllocator;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import org.springframework.core.env.Environment;

import java.net.InetAddress;
import java.net.UnknownHostException;

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class SpringbootNettyGpsJt808 {

    public static void main(String[] args) throws UnknownHostException, InterruptedException {
        ConfigurableApplicationContext context = SpringApplication.run(SpringbootNettyGpsJt808.class, args);

        //打印swagger地址
        String ip = InetAddress.getLocalHost().getHostAddress();
        String port = context.getBean(Environment.class).getProperty("server.port");
        System.out.println("Swagger2接口地址:" + "http://" + ip + ":" + port + "/doc.html");
        new Thread(() -> {
            try {
                MyNettyServer server = new MyNettyServer();
                server.serverStart();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }).start();
        //循环推送测试
//        while (true){
//            Thread.sleep(10000);
//            System.out.println("循环发送报文到设备。。。当前设备数量"+MyNettyServer.map.size());
//            //读取全部ai
//            ByteBuf aiBuff = PooledByteBufAllocator.DEFAULT.directBuffer();
//            byte[] diData = "ok".getBytes();
//            aiBuff.writeBytes(diData);
//            MyNettyServer.clients.writeAndFlush(aiBuff);//全部通道发送ai查询
//
//
//            //读取全部do
//            ByteBuf doBuff = PooledByteBufAllocator.DEFAULT.directBuffer();
//            byte[] doData = "ok".getBytes();
//            doBuff.writeBytes(doData);//获取字节数组
//            MyNettyServer.clients.writeAndFlush(doBuff);//全部通道发送do查询
////            MyNettyServer.map.get("").channel().writeAndFlush(byteBuf);//单个通道发送
//        }
    }

}
